package com.alibaba.user.service.impl;

import com.alibaba.basic.constant.BaseConstants;
import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.util.MD5Utils;
import com.alibaba.basic.util.StrUtils;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.domain.User;
import com.alibaba.user.dto.UserDto;
import com.alibaba.user.mapper.LogininfoMapper;
import com.alibaba.user.mapper.UserMapper;
import com.alibaba.user.service.IUserService;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import jdk.nashorn.internal.runtime.options.LoggingOption;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private LogininfoMapper logininfoMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public void phoneRegister(UserDto userDto) {
        String phone = userDto.getPhone();
        String phoneCode = userDto.getPhoneCode();
        String password = userDto.getPassword();
        String passwordRepeat = userDto.getPasswordRepeat();

        //1.校验 - 空值校验
        if (StringUtils.isEmpty(phone)||StringUtils.isEmpty(phoneCode)||StringUtils.isEmpty(password)||StringUtils.isEmpty(passwordRepeat))
        {
            throw new BusinessException("相关参数不可为空");
        }
        //2.校验 - 密码一致校验
        //trim()方法用于删除字符串的头尾空白符，空白符包括：空格、制表符 tab、换行符等其他空白符等。
        if (!password.trim().equals(passwordRepeat.trim())){
            throw new BusinessException("两次密码不一致");
        }

        //3.校验 - 验证码是否过期 :  9527:441122222
        Object redisPhoneCode = redisTemplate.opsForValue().get(BaseConstants.VerifyCodeConstant.BUSINESS_REGISTER_KEY + phone);
        if (redisPhoneCode == null){
            throw new BusinessException("手机验证码过期,请重新发送验证码");
        }
        //校验验证码是否错误
        if (!redisPhoneCode.toString().split(":")[0].equalsIgnoreCase(phoneCode)){
            throw new BusinessException("验证码错误");
        }
        //5.添加 - t_logininfo信息
        User user = userDto2User(userDto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        //6.添加 - t_user信息
        //设置登录信息id
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    // BeanUtils.copyProperties("转换前的类", "转换后的类");
    private Logininfo user2Logininfo(User user){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    private User userDto2User(UserDto userDto){
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());

        //加盐
        //getComplexRandomString 获取随机字符串
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        //加密(密码先加盐 然后加盐后的密码通过md5技术加密)
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(md5Pwd);
        return user;
    }



}
